package com.rfid.base.base;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

public abstract class BaseListFragment<T extends BaseAdapter,Y,G extends BasePresenter> extends BaseFragment<G> implements Listener<Y> {

    protected List<Y> mData = new ArrayList<>();
    protected RecyclerView mRecyclerView;
    protected T mAdapter;
    protected Listener<Y> mListener;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mAdapter = getAdapter();
        mAdapter.setListener(this);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRecyclerView = view.findViewById(getRecyclerViewId());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRecyclerView.setAdapter(mAdapter);
    }

    public void setListener(Listener g) {
        mListener = g;
    }

    @Override
    public void onClick(Y y) {
        if (mListener != null) {
            mListener.onClick(y);
        }
    }

    @Override
    public void onLongClick(Y y) {

    }

    @Override
    public void onSelect(Y y, boolean select) {

    }

    protected abstract T getAdapter();

    protected abstract int getRecyclerViewId();

    public void setData(List<Y> books) {
        System.out.println("setData");
        if (books != mData) {
            mData.clear();
            mData.addAll(books);
        }
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }

    public void addData(List<Y> data) {
        mData.addAll(data);
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
        }
    }

}
